var url_search="/web/api/product/productSale/search_productSale";
$(function(){
	  //1.初始化Table
    var oTable = new TableInit();
    oTable.Init();
});
var TableInit = function () {
	var oTableInit = new Object();
	 oTableInit.Init = function () {
		 var url = producturl + url_search;
		 $("#tbody_list_table").empty();
		 $("#product_pc").empty();
		 $.ajax({
			  url: url,
			  dataType:'json',
			  type:'post',
			 xhrFields: {
				 withCredentials: true
			 },
			 crossDomain: true,
		      data:{    
		    	"product_id": $.trim($("#product_id").val()),
		    	"product_type": $.trim($("#product_type").val()),
		    	"status": $.trim($("#status").val()),
		    	"product_name": $.trim($("#product_name").val()),
				 "three_type":$("#three_type").val(),
		    	"NEED_DO_CHECK": "false"
		      },
			  success: function (data) {
				  if (data != null && data.result.listProductSale.length>0) {
					  setProductTableList(data);
					   var pc = data.result.pageController; //取到pageCount的值(把返回数据转成object类型)
					   var currentPage = pc.currentPage; //得到currentPage
					   var pageCount = pc.totalPages; //得到totalPages
					   var pageSize = pc.pageSize; //得到totalPages
					   var options = {
						   bootstrapMajorVersion: 3, //版本
						   currentPage: currentPage, //当前页数
						   totalPages: pageCount, //总页数
						   itemTexts: function (type, page, current) {
							    switch (type) {
								    case "first":
								     return "首页";
								    case "prev":
								     return "上一页";
								    case "next":
								     return "下一页";
								    case "last":
								     return "末页";
								    case "page":
							        return page;
					            }
					      },//点击事件，用于通过Ajax来刷新整个tbody_list_table列表
						   onPageClicked: function (event, originalEvent, type, page) {
							    $.ajax({
								    url: producturl + url_search ,
								    type: "post",
									xhrFields: {
										withCredentials: true
									},
									crossDomain: true,
								    data:{    
								    	"product_id": $.trim($("#product_id").val()),
								    	"product_type": $.trim($("#product_type").val()),
								    	"status": $.trim($("#status").val()),
								    	"product_name": $.trim($("#product_name").val()),
										"three_type":$("#three_type").val(),
								    	"NEED_DO_CHECK": "false",
								    	"pageNo":page,
								    	"pageSize":pageSize
								      },  
							        success: function (data1) {
										if (data1 != null) {
											setProductTableList(data1);
										}
									}
							       });
							   }
					   };
					   $("#product_pc").bootstrapPaginator(options);
				  }
			  }
		 });
	 }
	 return oTableInit;
}

function setProductTableList(data){
	if (data != null && data.result.listProductSale.length>0) {
		var tablelistProduct = new Array();
		$.each(data.result.listProductSale, function (index, item) { //遍历返回的json
			tablelistProduct.push('<tr>');
			var receiveDate = "";
			var receiveTimes = "";
			if(item.three_type == 400 && item.my_attr_json != ""){
				var myAttrJson = eval('(' +item.my_attr_json+ ')');
				if(myAttrJson != null){
					receiveDate  = myAttrJson[0].attr_value;
					receiveTimes = myAttrJson[1].attr_value;
				}
			}
			tablelistProduct.push('<td><input type="checkbox" value="'+ item.id + '_' + item.name + '_' + item.price + '_'+ receiveDate + '_'+ receiveTimes+ '_'+ item.product_type + '_' + item.two_type + '_' + item.three_type + '" ></td>');
			tablelistProduct.push('<td>' + (index + 1) + '</td>');
			tablelistProduct.push('<td>' + item.id + " <input type='hidden' id='attr_" + item.id + "' value='" + item.my_attr_json + "' />" + '</td>');
			// switch (item.product_type) {
			// 	case 100:
			// 		tablelistProduct.push('<td>虚拟</td>');
			// 		break;
			// 	case 200:
			// 		tablelistProduct.push('<td>实物</td>');
			// 		break;
			// 	default:
			// 		tablelistProduct.push('<td>未标识</td>');
			// }
			tablelistProduct.push('<td>' + item.name + '</td>');
			tablelistProduct.push('<td>' + item.price.toFixed(2) + '</td>');
			tablelistProduct.push('<td>' + receiveDate + '</td>');
			tablelistProduct.push('<td>' + receiveTimes + '</td>');
			tablelistProduct.push('</tr>');
		});
		$("#tbody_list_table").html(tablelistProduct.join(''));
	}

}

